//index.js

Page({
  data: {
    terms:'',
    selecttype: ['全部', '求购', '供应','招商', '商服'],
    selecttypeindex:'0',
    searchData:[],
  },
  onLoad: function () {
    
  },
  onShow: function () {
    var that = this;
    var searchData = wx.getStorageSync('searchTradeData');
    console.log(searchData)
    that.setData({
      searchData: searchData
    })
  },
  //选择搜索分类
  bindPickerChange: function (e) {
    var that = this
    console.log('picker发送选择改变，携带值为', e.detail.value)
    that.setData({
      selecttypeindex: e.detail.value
    })
  },

  searchlink:function(e){
    var that = this
    var v = e.detail.value
    that.setData({
      terms:v
    })
  },
 search:function (e) {
    var that = this
    var ky = this.data.terms;
    if (ky != '') {
      var searchData = wx.getStorageSync('searchTradeData') || []

      var hasone=searchData.indexOf(ky)
      if(hasone!==-1){
        searchData.splice(hasone, 1)
      }
      searchData.unshift(ky)
      wx.setStorageSync('searchTradeData', searchData)
      wx.redirectTo({
        url: '/pages/weishangji/search-result/search-result?terms='+that.data.terms+'&type='+that.data.selecttypeindex
      })
    }else{
      wx.redirectTo({
        url: '/pages/weishangji/search-result/search-result?terms='
      })
    }
  },
  deleteHistory: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '是否删除历史搜索',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync('searchTradeData', []);
          that.setData({
            searchData: []
          })
        }
      }
    })
  }
  
})
